function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

var lyTexti = lyText;
function load(){
    var lyMain = document.getElementById("lyMain");
    var lyLength = lyText.length;
    for (let i = 0; i < lyLength; i++) {
        let pText = lyText[i];
        var pLength = pText.length;
        let text = "";
        for (let j = 0; j < pLength; j++) {
            text += pText[j].text
        }
        var pele = document.createElement("p");
        pele.innerText = text;
        lyMain.appendChild(pele);
    }
    setLyTop()
    play()

}
var pThis = 0;
async function play(){
  await sleep(1000);
    // 获取 id 为 "lyMain" 的 div 元素
    var lyMainDiv = document.getElementById("lyMain");
    var pTags = lyMainDiv.getElementsByTagName("p");
    while (pThis < pTags.length){
        let text = pTags[pThis].textContent;
        let pText = lyTexti[pThis];
        let lyTextP = "";
        for (let i = 0; i < pText.length; i++) {
            lyTextP += "<span>"+ pText[i].text+"</span>";
        }
        pTags[pThis].innerHTML = lyTextP;
        pTags[pThis].classList.add("highlight");
        var spanTag = pTags[pThis].getElementsByTagName("span");
        for (var c = 0; c < spanTag.length; c++){
            var spanTagElement = spanTag[c];
            let j = 0;
            let lastNum = 0;
            while (j <= 100){
                spanTagElement.style.backgroundImage = "linear-gradient(to right, lightgreen "+j+"%,#efedef 0%)";
                j += 4
                let mm = pText[c].time/26;
                mm += lastNum;
                lastNum = mm - parseInt(mm);
                mm -= lastNum;
                await sleep(mm);
            }
        }

        pTags[pThis].innerText = text;
        pTags[pThis].classList.remove("highlight");
        pThis++;
        let he = parseInt(window.getComputedStyle(pTags[pThis]).height)
        lyMainDiv.style.top = (parseInt(window.getComputedStyle(lyMainDiv).top) - he) + "px";
    }
}
var mainDiv = document.getElementById("Main");
var lyMainHeight = parseInt(window.getComputedStyle(mainDiv).height)

function setLyTop(){
    var lyMainDiv = document.getElementById("lyMain");
    // 获取 lyMainDiv 中的所有 p 标签
    var pTags = lyMainDiv.getElementsByTagName("p");
    var pTagHeight = parseInt(window.getComputedStyle(pTags[pThis]).height)
    var lyHe = parseInt(lyMainHeight/pTagHeight)/2 ;
    lyMainDiv.style.top =  (pTagHeight * lyHe) + "px"
}
var resizeTimer = setTimeout(setLyTop,100);
window.addEventListener('resize', function() {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(function (){
        var lyMainDiv = document.getElementById("lyMain");
        let number = parseInt(window.getComputedStyle(mainDiv).height);
        let number1 = lyMainHeight - number;
        var pTags = lyMainDiv.getElementsByTagName("p");
        let pTagHeight = parseInt(window.getComputedStyle(pTags[pThis]).height)
        var lyHe = parseInt(number1/pTagHeight)/2 ;
        lyMainDiv.style.top = (parseInt(window.getComputedStyle(lyMainDiv).top) - lyHe * pTagHeight) + "px";
        lyMainHeight = number;
    },200);
});
window.addEventListener("wheel", function(event) {
  let initTop = parseInt(window.getComputedStyle(document.getElementById("lyMain")).top);
  console.log(initTop);
  // 判断鼠标滚轮滚动方向
  if (event.deltaY < 0) {
    // 向上滚动，减小top值
    initTop += 100;
  } else {
    // 向下滚动，增加top值
    initTop -= 100;
  }
  console.log(initTop);
  // 确保top值不会小于0

  lyMain.style.top = initTop + "px";
});
var lyMain = document.getElementById("lyMain");
var isDragging = false; // 判断是否正在拖动
var startY; // 记录鼠标点击的初始位置
var startTop; // 记录lyMain的初始top值

lyMain.addEventListener("mousedown", function(event) {
  isDragging = true;
  startY = event.clientY;
  startTop = parseFloat(lyMain.style.top) || 0; // 获取当前的top值，如果没有设置则默认为0
});

window.addEventListener("mouseup", function() {
  isDragging = false;
});

window.addEventListener("mousemove", function(event) {
  if (isDragging) {
    var deltaY = event.clientY - startY; // 计算鼠标移动的距离
    var newTop = startTop + deltaY; // 计算新的top值
    lyMain.style.top = newTop + "px";
  }
});
load()
